<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="400" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="头像" prop="avatar">
				<sc-upload
					v-model="form.avatar"
					:apiObj="apiObj"
					title="上传头像"
				></sc-upload>
			</el-form-item>
			<el-form-item label="排序号" prop="sortNo">
				<el-input-number v-model="form.sortNo" controls-position="right" :min="1" style="width: 100%;"></el-input-number>
			</el-form-item>
			<el-form-item label="关联账号" prop="accountId">
				<select-user-dialog v-model:accountId="form.accountId" :mode="false"/>
			</el-form-item>
			<el-form-item label="驾照类型" prop="licenceType">
				<sc-select v-model="form.licenceType" :params="{code:'licenceType'}" :apiObj="$API.system.sysDic.getSysDicByCode" clearable filterable style="width: 100%"></sc-select>
			</el-form-item>
			<el-form-item label="发证日期" prop="issueYear">
				<el-date-picker v-model="form.issueYear" placeholder="发证日期" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
			</el-form-item>
			<el-form-item label="当前状态" prop="status">
			<el-radio-group v-model="form.status">
				<el-radio-button label="0">空闲</el-radio-button>
				<el-radio-button label="1">途中</el-radio-button>
				<el-radio-button label="2">休假</el-radio-button>
			</el-radio-group>
			</el-form-item>
			<el-form-item label="备注说明">
				<el-input type="textarea" :rows="2" maxlength="180" placeholder="备注说明" v-model="form.remark"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
import SelectUserDialog from "@/views/module/selectUserDialog";
export default {
	components: {
		SelectUserDialog
	},
	emits: ['success', 'closed'],
	data() {
		return {
			apiObj:this.$API.file.uploadImg,
			mode: "add",
			titleMap: {
				add: '创建司机对话框',
				edit: '编辑司机对话框'
			},
			form: {
				driverId:'',
				sortNo:0,
				avatar:'',
				accountId: "",
				licenceType:"",
				issueYear: "",
				remark: "",
				status: "0"
			},
			rules: {
				accountId: [
					{required: true, message: '关联账号不能为空'}
				],
				licenceType: [
					{required: true, message: '驾照类型不能为空'}
				],
				issueYear: [
					{required: true, message: '发证日期不能为空'}
				]
			},
			visible: false,
			isSaveing: false,
		}
	},
	mounted() {

	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					var res;
					if(this.mode=="add")
					{
						this.isSaveing = true;
						res = await this.$API.office.vehicle.insertVehicleDriver.post(this.form);
						this.isSaveing = false;
					}else if(this.mode=="edit")
					{
						this.isSaveing = true;
						res = await this.$API.office.vehicle.updateVehicleDriver.post(this.form);
						this.isSaveing = false;
					}
					if (res.code == 200) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		//表单注入数据
		setData(data) {
			this.form.driverId = data.driverId
			this.form.accountId = data.accountId
			this.form.sortNo = data.sortNo
			this.form.avatar = data.avatar
			this.form.licenceType = data.licenceType
			this.form.issueYear = data.issueYear
			this.form.remark = data.remark
			this.form.status = data.status
		}
	}
}
</script>

<style>
</style>
